package com.dupengcheng.subject.domain.hanler.subject;

import com.dupengcheng.subject.common.enums.IsDeleteEnum;
import com.dupengcheng.subject.common.enums.SubjectTypeEnum;
import com.dupengcheng.subject.domain.convert.SubjectAnswerConvert;
import com.dupengcheng.subject.domain.entity.SubjectAnswerBO;
import com.dupengcheng.subject.domain.entity.SubjectInfoBO;
import com.dupengcheng.subject.domain.entity.SubjectOptionBO;
import com.dupengcheng.subject.infra.basic.entity.SubjectMultiple;
import com.dupengcheng.subject.infra.basic.entity.SubjectRadio;
import com.dupengcheng.subject.infra.basic.service.SubjectMultipleService;
import com.dupengcheng.subject.infra.basic.service.SubjectRadioService;
import lombok.Getter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class MultipleHandler implements SubjectTypeHandler {
    @Getter
    private SubjectTypeEnum subjectTypeEnum=SubjectTypeEnum.MULTIPLE;

    @Resource
    private SubjectMultipleService multipleService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //先添加INfO表、再添加Mapping表、最后加单选表。
        List<SubjectMultiple> subjectMultipleList=new LinkedList<>();
        for (SubjectAnswerBO subjectAnswerBO : subjectInfoBO.getOptionList()) {
            SubjectMultiple subjectMultiple=new SubjectMultiple();
            subjectMultiple.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
            subjectMultiple.setOptionContent(subjectAnswerBO.getOptionContent());
            subjectMultiple.setIsCorrect(subjectAnswerBO.getIsCorrect());
            subjectMultiple.setOptionType(subjectAnswerBO.getOptionType());
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultipleList.add(subjectMultiple);
        }

        multipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        //查询多选
        SubjectMultiple subjectMultiple=new SubjectMultiple();
        subjectMultiple.setIsDeleted(IsDeleteEnum.UN_DELETE.getCode());
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple> subjectMultipleList=multipleService.queryByCondtion(subjectMultiple);
        List<SubjectAnswerBO> subjectAnswerBOS= SubjectAnswerConvert.INSTANCE
                .convertMultipleToAnswerList(subjectMultipleList);
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
